﻿//========================================================= 
/**@file ICBC_HidIDCardTCmdAdapter.h 
 * @brief 工行HID二代证阅读器指令集适配器 
 *
 * 指令中不包含"AA AA AA 69 96"指令头,发送时剔除,接收时过滤即可 
 * 
 * @date 2014-11-06   22:27:28 
 * @author Zhyioo 
 * @version 1.0
 */ 
//--------------------------------------------------------- 
#ifndef _LIBZHOUYB_ICBC_HIDIDCARDCMDADAPTER_H_
#define _LIBZHOUYB_ICBC_HIDIDCARDCMDADAPTER_H_
//--------------------------------------------------------- 
#include "../../include/Base.h"
//---------------------------------------------------------
namespace zhou_yb {
namespace application {
namespace idcard {
//--------------------------------------------------------- 
/// 工行二代证指令适配器
class ICBC_HidIDCardCmdAdapter : 
    public IInteractiveTrans,
    public BaseDevAdapterBehavior<IInteractiveTrans>,
    public RefObject
{
public:
    //----------------------------------------------------- 
    /// 写数据 
    virtual bool Write(const ByteArray& data)
    {
        if(!IsValid())
            return false;

        // 前导指令长度 
        const size_t CMD_LEN = 5;
        return _pDev->Write(data.SubArray(CMD_LEN));
    }
    //----------------------------------------------------- 
    /// 读数据 
    virtual bool Read(ByteBuilder& data)
    {
        const byte IDC_CMD[] = { 0xAA, 0xAA, 0xAA, 0x96, 0x69, 0x00, 0x00 };
        ByteArray idCmdArray(IDC_CMD, SizeOfArray(IDC_CMD));
        size_t lastLen = data.GetLength();
        data += idCmdArray;
        if(!_pDev->Read(data))
        {
            data.RemoveTail(data.GetLength() - lastLen);
            return false;
        }
        size_t len = data.GetLength() - lastLen - idCmdArray.GetLength();
        // 设置长度位
        data[lastLen + 5] = _itobyte(len >> BIT_OFFSET);
        data[lastLen + 6] = _itobyte(len);
        return true;
    }
    //----------------------------------------------------- 
};
//--------------------------------------------------------- 
} // namespace idcard
} // namespace application 
} // namespace zhou_yb 
//--------------------------------------------------------- 
#endif // _LIBZHOUYB_ICBC_HIDIDCARDCMDADAPTER_H_
//=========================================================
